package com.util;
import java.util.List;
import org.springframework.context.ApplicationContext;

import com.application.department.entity.Department;
import com.application.department.service.DepartmentService;
import com.application.function.entity.Function;
import com.application.function.entity.Menu;
import com.application.function.service.FunctionService;
import com.application.mac.service.MacService;
import com.application.mac.struts.form.MacForm;
import com.application.model.entity.Model;
import com.application.model.service.ModelService;
import com.application.model.struts.form.ModelForm;
import com.application.role.entity.Role;
import com.application.role.service.RoleService;
import com.application.role.struts.form.RoleForm;
import com.application.sys.entity.Sys;
import com.application.sys.service.SysService;
import com.application.sys.struts.form.SysForm;
import com.application.usertable.entity.Usertable;
import com.application.usertable.service.UsertableService;
import com.application.yfsmsrecord.service.YfSmsrecordService;

public class ApplicationUtil {
	
	public ApplicationUtil(){
	}
    public String getWebClassesPath() {    
        String path = getClass().getProtectionDomain().getCodeSource()    
                .getLocation().getPath().substring(1);    
       return path;    
  
    }
   public String getWebInfPath() {    
        String path = getWebClassesPath();    
       if (path.indexOf("WEB-INF") > 0) {    
            path = path.substring(0, path.indexOf("WEB-INF") + 8);    
        } 
       return path;    
    }  
   
   	/**
   	 * 根据id获得模块对象
   	 * @param id
   	 * @return
   	 */
	public Model getModelById(String id) {
		ModelService modelService= (ModelService) SpringBeanFactory.getInstance().getBean("modelService");
		ModelForm modelForm = new ModelForm();
		modelForm.setId(id);
		List listtemp = modelService.searchModel(modelForm);
		if(listtemp!=null&&listtemp.size()>0){
			return  (Model)listtemp.get(0);
		}
		else{
			return null;
		}
		
	}
	
   	/**
   	 * 根据fid获得模块子模块集合
   	 * @param id
   	 * @return
   	 */
	public List<Model> getModelListByFid(String fid) {
		ModelService modelService=(ModelService) SpringBeanFactory.getInstance().getBean("modelService");
		return (List<Model>) modelService.searchModelMenu(fid);
	}
   	/**
   	 * 根据fid和uid获得模块子模块集合
   	 * @param id
   	 * @return
   	 */
	public List<Model> getModelListByFid(String fid,String uid) {
		ModelService modelService=(ModelService) SpringBeanFactory.getInstance().getBean("modelService");
		return (List<Model>) modelService.searchModelMenu(fid,uid);
	}
   	/**
   	 * 根据模块ID获得功能对象
   	 * @param id
   	 * @return
   	 */
	public List<Function> getFunctionByMid(String mid) {
		FunctionService functionService=(FunctionService) SpringBeanFactory.getInstance().getBean("functionService");
		return (List<Function>) functionService.searchFunctionMenu(mid);
	}
   	/**
   	 * 根据模块ID获得功能对象
   	 * @param id
   	 * @return
   	 */
	public List<Menu> getFunctionByMid(String mid,String uid) {
		FunctionService functionService=(FunctionService)SpringBeanFactory.getInstance().getBean("functionService");
		return (List<Menu>) functionService.searchFunctionMenu(mid,uid);
	}
		
   	/**
   	 * 根据用户ID用户角色对象
   	 * @param id
   	 * @return
   	 */
	public List<Role>  getUserRole(String uid) {
		UsertableService usertableService=(UsertableService) SpringBeanFactory.getInstance().getBean("usertableService");
		return usertableService.getUserRole(uid);
	}
	
   	/**
   	 * 根据获得所有系统角色
   	 * @param id
   	 * @return
   	 */
	public List<Role>  getAllRole() {
		RoleService roleService=(RoleService) SpringBeanFactory.getInstance().getBean("roleService");
		RoleForm roleForm = new RoleForm();
		return roleService.searchRole(roleForm);
	}
   	/**
   	 * 根据角色名获得角色对象
   	 * @param id
   	 * @return
   	 */
	public Role  getAllRoleByName(String name) {
		RoleService roleService=(RoleService) SpringBeanFactory.getInstance().getBean("roleService");
		RoleForm roleForm = new RoleForm();
		roleForm.setName(name);
		Role r = new Role();
		List<Role> temp = roleService.searchRole(roleForm);
		if(temp!=null&&temp.size()>0){
			r = temp.get(0);
		}
		return r;
	}
   	/**
   	 * 根据传入参数的ID获得系统参数 
   	 * @param id (1:网卡认证；2：短信发送)
   	 * @return
   	 */
	public String  getSys(String id) {
		SysService sysService=(SysService) SpringBeanFactory.getInstance().getBean("sysService");
		SysForm sysForm = new SysForm();
		sysForm.setId(id);
		Sys s = (Sys) sysService.searchSys(sysForm).get(0);
		return s.getCode();
	}	
   	/**
   	 * 根据传入参数的角色ID获得人员
   	 * @param id 
   	 * @return
   	 */
	public List<Usertable>  getUserById(String rid) {
		UsertableService usertableService=(UsertableService)SpringBeanFactory.getInstance().getBean("usertableService");
		return usertableService.getUserById(rid);
	}
   	/**
   	 * 根据传入参数的角色ID和登录用户部门ID获得人员
   	 * @param id 
   	 * @return
   	 */
	public List<Usertable>  getUserById(String rid,String oid) {
		UsertableService usertableService=(UsertableService)SpringBeanFactory.getInstance().getBean("usertableService");
		return usertableService.getUserById(rid,oid);
	}
   	/**
   	 * 根据传入参数的部门ID获得人员
   	 * @param id 
   	 * @return
   	 */
	public List<Usertable>  getUserByIdBm(String oid) {
		UsertableService usertableService=(UsertableService)SpringBeanFactory.getInstance().getBean("usertableService");
		return usertableService.getUserByIdBm(oid);
	}
   	/**
   	 * 根据传入参数的角色ID和登录用户部门ID获得人员
   	 * @param id 
   	 * @return
   	 */
	public List<Usertable>  getUserByName(String rname,String oid) {
		UsertableService usertableService=(UsertableService) SpringBeanFactory.getInstance().getBean("usertableService");
		return usertableService.getUserByName(rname,oid);
	}
   	/**
   	 * 根据人员ID获得本部门其他人员对象列表
   	 * @param id 
   	 * @return
   	 */
	public List<Usertable> getOrgUserserById(String uid){ 
		UsertableService usertableService=(UsertableService)SpringBeanFactory.getInstance().getBean("usertableService");
		return usertableService.getOrgUserserById(uid);
	}
	
	/**
   	 * 获取部门集合
   	 * @param id 
   	 * @return
   	 */
	public List<Department> getOrgList(){ 
		DepartmentService departmentService=(DepartmentService)SpringBeanFactory.getInstance().getBean("departmentService");
		return departmentService.searchDepartment(null);
	}

	/**
   	 * 获取部门
   	 * @param id 
   	 * @return
   	 */
	public Department getOrg(String id){ 
		DepartmentService departmentService=(DepartmentService)SpringBeanFactory.getInstance().getBean("departmentService");
		Department query = new Department();
		query.setId(id);
		return departmentService.getDepartment(query);
	}
	
	
	/**
   	 * 更改短信发送状态
   	 * @param id 
   	 * @return
   	 */
	public void updateSmsState(int id,Long state){ 
		YfSmsrecordService yfSmsrecordService=(YfSmsrecordService)SpringBeanFactory.getInstance().getBean("yfSmsrecordService");
	    yfSmsrecordService.updateStatus(id, state);
	}
	
}
